<?php


namespace app\admin\controller;


use app\common\model\MoneyLogModel;
use app\common\model\RechargeModel;
use app\common\model\UserModel;
use cmf\controller\AdminBaseController;
use think\db\Query;
use think\Request;

class RechargeController extends AdminBaseController
{
    public function index(Request $request)
    {
        $list = RechargeModel::where(function (Query $query) use ($request) {
            if (!empty($request->keyword)) {
                $query->where('order_no|nickname', 'like', "%{$request->keyword}%");
            }
            if (isset($request->status) && $request->status == 1) {
                $query->where('status', $request->status);
            }
        })->order('id desc')->paginate();
        return $this->fetch('index', ['list' => $list]);
    }

    public function del($id)
    {
        if (RechargeModel::destroy($id)) {
            $this->success('ok');
        } else {
            $this->error('失败');
        }
    }

    public function batchdel($ids)
    {
        if (RechargeModel::destroy($ids)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function batchdels($ids)
    {
        if (MoneyLogModel::destroy($ids)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function list(Request $request)
    {
        $list = MoneyLogModel::where(function (Query $query) use ($request) {
            if (!empty($request->userid)) {
                $query->where('user_id', $request->userid);
            }
            if (!empty($request->type)) {
                $query->where('type', $request->type);
            }
        })->order('id desc')->paginate()
            ->each(function ($v, $k) {
                $user = UserModel::find($v['user_id']);
                $v['nickname'] = $user['user_nickname'];
                $v['phone'] = $user['user_login'];
            });

        $this->assign('list', $list);
        $this->assign('type', MoneyLogModel::getType());
        return $this->fetch();
    }
}